<?php

namespace framework\system\kernel;

use framework;

class CFile extends CModel {

    public static function requireFile($file = NULL, $_Return = FALSE) {

        if (is_array($file)) {
            foreach ($file as $key => $value) {
                $fileInArray = $value;
                self::requireFile($fileInArray, $_Return);
            }
        }
        if (!is_string($file)) {
            return FALSE;
        }
        $file = str_replace("\\", DS, $file);

        if (file_exists($file)) {

            if ($_Return) {
                return require_once $file;
            } else {
                require_once $file;
                return TRUE;
            }
        }
        return FALSE;
    }

    public static function fileExits($file) {
        if (file_exists($file) && is_file($file)) {
            return TRUE;
        }
        return FALSE;
    }

    public static function dirExits($dir) {
        if (file_exists($file) && is_dir($file)) {
            return TRUE;
        }
        return FALSE;
    }

    public static function fileInclude($file, $once = TRUE) {
        if ($once) {
            return include_once $file;
        }
        return include $file;
    }

    public static function fileWrite($path, $content) {
        
    }

    public static function fileGetContent($file) {

        return file_get_contents($file);
    }

    public static function checkFileName($path) {
        if (preg_match('/[\/\|\?\*\:\<\>\'\"\\\]+/', $path)) {
            return FALSE;
        }
        return true;
    }

    public static function checkPathName($path) {
        if (preg_match('/[\|\?\*\:\<\>\'\"]+/', $path)) {
            return FALSE;
        }
        return true;
    }

    /**
     * 建立目录树
     * @param type $path
     * @param type $mode
     * @return boolean
     */
    public static function mkdir($path = null, $mode = 0755) {

        if (!$path || !static::checkPathName($path)) {
            return FALSE;
        }
        if (is_dir($path)) {
            return FALSE;
        }
        $path = preg_replace('/\\\+/', '/', $path);
        $path_array = explode("/", $path);
        $_path_array = array();
        for ($i = 0; $i <= count($path_array); $i++) {
            if (!empty($path_array[$i])) {
                $_path_array[] = $path_array[$i];
            }
        }
        $_now = "";
        for ($i = 0; !empty($_path_array[$i]); $i++) {
            $_now.= $_path_array[$i] . DS;
            if (is_dir($_now)) {
                continue;
            } else {
                mkdir($_now, $mode);
            }
        }
    }

}
